<template>
	<view id="app">
		<view class="suosou">
			<image src="@/static/public/search.png" mode="widthFix" style="width: 1em;"></image>
			<input type="text" placeholder="搜索动态" style="width: 80%;">
			<uni-icons size="20" class="button " style="font-size: 13px;color: #FF5721 ;"> 搜索 </uni-icons>
		</view>
		<view class="body">
			<u-tabs :list="navigation" lineWidth="30" lineColor="#f56c6c" :activeStyle="{
			            color: '#303133',
			            fontWeight: 'bold',
			            transform: 'scale(1.05)'
			        }" :inactiveStyle="{
			            color: '#606266',
			            transform: 'scale(1)'
			        }" itemStyle="padding-left: 7px; padding-right: 7px; height: 34px;" :current="tab" @click="tabsCLick">
			</u-tabs>
		</view>
		<!-- 论坛帖子 -->
		<view class="forum-tiezi" v-for="(item,index) in newList" :key="index">
			<view>
				<u-row justify="space-between" gutter="5">
					<u-col span="1.5" style="height: 6vh;">
						<view class="demo-layout-bg-purple">
							<image :src="item.user.avatar" mode="aspectFill"></image>
						</view>
					</u-col>
					<u-col span="7" style="height: 6vh;">
						<view class="demo-layout bg-purple-light">
							<view style="font-size: 15px; margin: 3px  0 0 0;">{{item.user.name}}</view>
							<view>
								<view v-if="item.user.sex==0">
									<view style="color: #797879;font-size: 12px;">{{getSR(item.user.birthday)}}岁</view>
								</view>
								<view v-if="item.user.sex==1"
									style="display: flex; flex-wrap: wrap; margin: 3px  0 0 0;">
									<image src="@/static/public/male.png" style="width: 16px;height: 16px;"></image>
									<view style="color: #797879;font-size: 12px;">{{getSR(item.user.birthday)}}岁</view>
								</view>
								<view v-if="item.user.sex==2"
									style="display: flex; flex-wrap: wrap; margin: 3px  0 0 0;">
									<image src="@/static/public/woman.png" style="width: 16px;height: 16px;"></image>
									<view style="color: #797879;font-size: 12px;">{{getSR(item.user.birthday)}}岁</view>
								</view>
							</view>
						</view>
					</u-col>
					<u-col span="3" style="height: 6vh;">
						<view class="demo-layout1" @click="onConcern(index)" v-if="isThis(newList[index].user.id)">
							<view class="guanzhu1" v-if="item.isFollow">
								<image src="@/static/forum/correct.png"></image>
								已关注
							</view>
							<view class="guanzhu" v-else>
								<image src="@/static/forum/add.png"></image>
								关注
							</view>
							<view>
								<image src="@/static/public/under_the.png"
									style="width: 8px;height: 5px;margin:12px 0 0 2px;"></image>
							</view>
						</view>
					</u-col>
				</u-row>
			</view>
			<view style="width: 100%; margin: 7px" id="text">
				<u--text color="#615f60" size="13" :lines="3" :text="item.content"></u--text>
			</view>
			<view>
				<view class="u-page">
					<view class="u-demo-block">
						<view class="u-demo-block__content">
							<view class="album">
								<view class="album__content">
									<u-album :urls="item.picture" style="width: 100%;"></u-album>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view>
				<view class="add">
					<image src="@/static/public/clock.png"></image>
					<uni-dateformat :date="item.createTime" format="yyyy-MM-dd hh:mm:ss"> </uni-dateformat>
				</view>
				<view class="add">
					<image src="@/static/public/orientation_gray.png"></image>
					{{item.detailsAddress}}
				</view>
				<view style="width: 100%; height: 2px; border-bottom: 1rpx rgba(100, 100, 100, 0.1) solid ;"></view>
			</view>
			<view>
				<u-row justify="space-between" gutter="10">
					<u-col span="4">
						<view class="demo">
							<image src="@/static/forum/share.png"></image>
							分享
						</view>
					</u-col>
					<u-col span="4">
						<view class="demo">
							<image src="@/static/public/review.png"></image>
							{{item.reviewNum}}
						</view>
					</u-col>
					<u-col span="4" @click="onOperate(index)">
						<view class="demo" v-if="item.isPraise">
							<image src="@/static/forum/praise_select.png"></image>
							<view style="color: #ffa365;">{{item.praiseNum}}</view>
						</view>
						<view class="demo" v-else>
							<image src="@/static/forum/praise_none.png"></image>
							<view>{{item.praiseNum}}</view>
						</view>
					</u-col>
				</u-row>
			</view>
		</view>
		<view class="push" @click="ononld()">
			<image src="@/static/forum/publish.png"></image>
		</view>
		<view class="loading">
			<u-loading-icon v-if="(this.newList.length < this.total)"></u-loading-icon>
			<u-loadmore :loadmoreText="loadmore" dashed line v-else />
		</view>
	</view>
</template>

<script>
	import {
		rear
	} from '@/config/api.js';
	export default {
		data() {
			return {
				iSinfo: false,
				albumWidth: 0,
				newList: [],
				navigation: [{
					name: '关注',
				}, {
					name: '推荐',
				}, {
					name: '同城',
				}],
				tabs: 2,
				tab: 2,
				user: null,
				current: 0,
				pageSize: 5,
				city: "",
				userId: 0,
				total: 0,
				loadmore: "没有更多了"
			}
		},
		methods: {
			ononld() {
				if (uni.getStorageSync('user').id == null || uni.getStorageSync('user').id == "") {
					uni.showToast({
						title: '请登录',
						icon: 'none',
						duration: 2000,
					});
					uni.navigateTo({
						url: '/pages/mine/login'
					});
				} else {
					uni.navigateTo({
						url: '/pages/forum/publish'
					});
				}
			},
			isThis(id) {
				return this.user.id != id
			},

			getSR(birthday) {
				const time = new Date(birthday).getTime();
				const time2 = new Date().getTime();
				const a = time2 - time;
				const b = Math.floor(a / (1000 * 60 * 60 * 24 * 365))
				return b
			},
			onConcern(index) {
				if (uni.getStorageSync('user').id == null || uni.getStorageSync('user').id == "") {
					uni.showToast({
						title: '请登录',
						icon: 'none',
						duration: 2000,
					});
					uni.navigateTo({
						url: '/pages/mine/login'
					});
				} else {
					if (this.tabs != 0) {
						uni.request({
							url: rear + "/relation/controlsRelation",
							method: "POST",
							data: {
								"type": 0,
								"targetUserId": this.newList[index].user.id,
								"userId": this.user.id
							},
							success: (res) => {
								if (res.data.data) {
									const isFollow = !this.newList[index].isFollow;
									for (var i = 0; i < this.newList.length; i++) {
										if (this.newList[i].user.id == this.newList[index].user.id) {
											this.newList[i].isFollow = isFollow

										}
									}
								}
							}
						})
					}

				}
			},
			onOperate(index) {
				if (uni.getStorageSync('user').id == null || uni.getStorageSync('user').id == "") {
					uni.showToast({
						title: '请登录',
						icon: 'none',
						duration: 2000,
					});
					uni.navigateTo({
						url: '/pages/mine/login'
					});
				} else {
					uni.request({
						url: rear + "/praise/controlsPraise",
						method: "POST",
						data: {
							"contentId": this.newList[index].id,
							"type": 0,
							"userId": this.user.id
						},
						success: (res) => {
							if (res.data.data) {
								this.newList[index].isPraise = !this.newList[index].isPraise;
								if (this.newList[index].isPraise) {
									this.newList[index].praiseNum++;
								} else {
									this.newList[index].praiseNum--;
								}
							}
						}
					})
				}
			},
			getTweet() {
				if (this.tabs == 0) {
					uni.request({
						url: rear + "/tweet/getTweetByFollow",
						method: "GET",
						data: {
							current: this.current,
							pageSize: this.pageSize,
							userId: this.userId
						},
						success: (res) => {
							this.newList.push(...res.data.data);
							this.isThis();
						}
					})
				} else if (this.tabs == 1) {
					uni.request({
						url: rear + "/tweet/getTweetAllList",
						method: "GET",
						data: {
							current: this.current,
							pageSize: this.pageSize,
							userId: this.userId
						},
						success: (res) => {
							this.newList.push(...res.data.data);
							this.isThis();
						}
					})
				} else {
					uni.request({
						url: rear + "/tweet/getTweetByCity",
						method: "GET",
						data: {
							city: this.city,
							current: this.current,
							pageSize: this.pageSize,
							userId: this.userId
						},
						success: (res) => {
							this.newList.push(...res.data.data);
							this.isThis();
						}
					})
				}

			},
			showinfo() {
				this.iSinfo = !this.iSinfo
			},
			tabsCLick(item) {
				if (!(item.index == this.tabs)) {
					this.tabs = item.index;
					this.current = 0;
					this.pageSize = 5;
					this.newList = [];
					if (this.tabs == 0) {
						if (this.user == "" || this.user == null) {
							this.loadmore = "请先登录！"
							this.total = 0;
						} else {
							this.loadmore = "没有更多了"
							uni.request({
								url: rear + "/tweet/getTweetTotalByFollow",
								method: "GET",
								data: {
									userId: this.userId,
								},
								success: (res) => {
									this.total = res.data.data
									this.getTweet();
								}
							})
						}
					} else if (this.tabs == 1) {
						this.loadmore = "没有更多了"
						uni.request({
							url: rear + "/tweet/getTweetAllTotal",
							method: "GET",
							success: (res) => {
								this.total = res.data.data
								this.getTweet();
							}
						})
					} else {
						if (this.user !== "" && this.user != null) {
							this.loadmore = "没有更多了"
							uni.request({
								url: rear + "/tweet/getTweetTotalByCity",
								method: "GET",
								data: {
									city: this.city,
								},
								success: (res) => {
									this.total = res.data.data
									this.getTweet();
								}
							})
						} else {
							this.total = 0;
							this.loadmore = "请先登录！";
						}
					}
				}
			},
			load() {
				const promise = new Promise((resolve, reject) => {
					resolve(uni.getStorageSync('user'))
				});
				promise.then((user) => {
					this.user = user;
					this.navigation[2].name = user.city.name
					if (this.user == "") {
						this.loadmore = "请先登录！"
						this.total = 0
						this.current = 0;
						this.pageSize = 5;
						this.newList = [];
					} else {
						this.loadmore = "没有更多了"
						this.city = this.user.city.name;
						this.userId = this.user.id;
						uni.request({
							url: rear + "/tweet/getTweetTotalByCity",
							method: "GET",
							data: {
								city: this.city,
							},
							success: (res) => {
								if (this.total != res.data.data) {
									this.total = res.data.data
									this.current = 0;
									this.pageSize = 5;
									this.newList = [];
									this.getTweet();
								}
							}
						})

					}
				});
			}
		},

		onShow() {
			if (uni.getStorageSync('forumRefresh')) {
				uni.removeStorageSync('forumRefresh');
				this.tabs = 0;
				this.tab = 0;

				uni.request({
					url: rear + "/tweet/getTweetTotalByCity",
					method: "GET",
					data: {
						city: this.city,
					},
					success: (res) => {
						this.total = res.data.data
						this.current = 0;
						this.pageSize = 5;
						this.newList = [];
						this.getTweet();
					}
				})
			} else {
				if (this.user != uni.getStorageSync('user')) {
					// this.user = uni.getStorageSync('user');
					this.load()
				}
			}
		},

		onLoad() {
			// const promise = new Promise((resolve, reject) => {
			// 	resolve(uni.getStorageSync('user'))
			// });
			// promise.then((user) => {
			// 	this.user = user;
			// 	if (this.user == "") {
			// 		this.loadmore = "请先登录！"
			// 	} else {
			// 		this.loadmore = "没有更多了"
			// 		this.city = this.user.city.name;
			// 		this.userId = this.user.id;
			// 		uni.request({
			// 			url: rear + "/tweet/getTweetTotalByCity",
			// 			method: "GET",
			// 			data: {
			// 				city: this.city,
			// 			},
			// 			success: (res) => {
			// 				if (this.total != res.data.data) {
			// 					this.total = res.data.data
			// 					this.current = 0;
			// 					this.pageSize = 5;
			// 					this.newList = [];
			// 					this.getTweet();
			// 				}
			// 			}
			// 		})

			// 	}
			// });
			this.load()
		},
		onReachBottom() { //页面触底
			if (this.newList.length < this.total) {
				this.current++;
				this.getTweet();
			}
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
			if (this.user == "" && this.tabs != 1) {
				this.city = "";
				this.userId = "0";
				this.loadmore = "请先登录！"
				uni.stopPullDownRefresh();
			} else {
				this.loadmore = "没有更多了"
				if (this.user == "") {
					this.city = "北京";
					this.userId = "0";
				} else {
					this.city = this.user.city.name;
					this.userId = this.user.id;
				}
				if (this.tabs == 0) {
					uni.request({
						url: rear + "/tweet/getTweetTotalByFollow",
						method: "GET",
						data: {
							userId: this.userId,
						},
						success: (res) => {
							this.total = res.data.data
							this.current = 0;
							this.pageSize = 5;
							this.newList = [];
							this.getTweet();
						}
					})
				} else if (this.tabs == 1) {
					uni.request({
						url: rear + "/tweet/getTweetAllTotal",
						method: "GET",
						success: (res) => {
							this.total = res.data.data
							this.current = 0;
							this.pageSize = 5;
							this.newList = [];
							this.getTweet();
						}
					})
				} else {
					uni.request({
						url: rear + "/tweet/getTweetTotalByCity",
						method: "GET",
						data: {
							city: this.city,
						},
						success: (res) => {
							this.total = res.data.data
							this.current = 0;
							this.pageSize = 5;
							this.newList = [];
							this.getTweet();
						}
					})

				}
			}


		}
	}
</script>

<style lang="scss" scoped>
	#app {
		width: 100vw;
		height: 100vh;
		padding: 100rpx 35rpx 0 35rpx;
		background-image: url("@/static/public/background.png");
		background-size: 100% 100%;
		box-sizing: border-box;
		position: absolute;

		.push {
			position: fixed;
			bottom: 3vh;
			right: 4vw;

			image {
				width: 65px;
				height: 65px;
			}
		}

		.suosou {
			height: 25px;
			background-color: white;
			padding: 6px;
			display: flex;
			border-radius: 20px;
			flex-direction: row;
			align-items: center;
			justify-content: space-evenly;
			box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
		}

		.body {
			margin: 2vw 0 1vw 0;
		}

		.add {
			font-size: 10px;
			margin: 2px 0;
			color: #969396;
			display: flex;
			align-items: center;
			margin: 4px 0;

			image {
				width: 13px;
				height: 13px;
			}
		}

		.demo {
			color: #323132;
			font-size: 15px;
			margin: 2px 0;
			display: flex;
			margin: 1vh 0 0 0;
			justify-content: center;

			image {
				width: 13px;
				height: 13px;
				margin: 4px 1vw 0 1vw;
			}
		}

		.forum-tiezi {
			border-radius: 5px;
			padding: 1vh 2vw;
			margin: 0 0 1vh 0;
			box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);

			.demo-layout1 {
				display: flex;
				flex-wrap: wrap;

				.guanzhu {
					justify-content: center;
					width: 75%;
					height: 20px;
					border: 1px #ff6f02 solid;
					border-radius: 20px;
					margin: 3px;
					color: #ff6f02;
					font-size: 12px;
					line-height: 20px;
					text-align: center;

					image {
						width: 9px;
						height: 9px;

					}
				}

				.guanzhu1 {
					justify-content: center;
					width: 75%;
					height: 20px;
					font-size: 12px;
					line-height: 20px;
					margin: 3px;
					text-align: center;
					border: 1px #8a8a8a solid;
					border-radius: 20px;
					color: #8a8a8a;

					image {
						vertical-align: -1vw;
						width: 14px;
						height: 15px;

					}
				}
			}

			.demo-layout-bg-purple image {
				width: 5vh;
				height: 5vh;
				border-radius: 20px;

			}
		}

		.album {
			@include flex;
			align-items: flex-start;

			&__avatar {
				background-color: $u-bg-color;
				padding: 5px;
				border-radius: 3px;
			}

			&__content {
				margin-left: 10px;
				flex: 1;
			}
		}

	}

	//文本动态样式
	#text {
		.Express {
			display: flex;
			flex-direction: column;
			position: relative;

			.info {
				display: flex;
				flex-direction: column;

				view {
					text-align: justify;
					font-size: 14px;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: rgba(102, 102, 102, 1);
				}

				text {
					width: 70px;
					font-size: 14px;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 50%);
					color: #ff0000;
					position: absolute;
					bottom: 1upx;
					right: 1upx;
				}
			}

		}

		.hidebtn {
			display: flex;
			flex: 1;
			justify-content: flex-end;
			color: #ff0101;
			font-size: 13px;
		}

		.hide {
			word-break: break-word; //换行模式
			overflow: hidden;
			text-overflow: ellipsis; //修剪文字
			display: -webkit-box;
			-webkit-line-clamp: 2; //此处为上限行数
			-webkit-box-orient: vertical;
		}
	}

	.loading {
		text-align: center;
		height: 50px;
		image {
			width: 10vw;
		}
	}
</style>